const router = require("koa-router")();
const { muser } = require('../models/muser');
const { PassThrough } = require("stream");
router.get("/sse", async (ctx) => {
    ctx.request.socket.setTimeout(30 * 1000)
    ctx.req.socket.setNoDelay(true);
    ctx.req.socket.setKeepAlive(true);
    ctx.set({
        "Content-Type": "text/event-stream",
        "Cache-Control": "no-cache",
        "Connection": "keep-alive",
    });
    const stream = new PassThrough();
    ctx.status = 200;
    ctx.body = stream;
    //数据库查询用户信息
    const suser = await muser.find({}, '-__v').lean();
    setInterval(() => {
        stream.write(`data: ${JSON.stringify(suser)}\n\n`);
    }, 5000);
});

module.exports = router;
